/**
 * Created by Administrator on 2017/2/6.
 */


const http = require('http')
const iconv = require('iconv-lite');
const cheerio = require('cheerio');
const spider = require('./spider');


http.get('http://www.laoguo5.com/star/', (res) => {

    var chunks = []
    res.on('data', function (chunk) {
        chunks.push(chunk);
    });

    res.on('end', function () {
        var buf = Buffer.concat(chunks);
        var str = iconv.decode(buf, 'gbk');
        //console.log('BODY:', str);

        var $ = cheerio.load(str);

        //console.log($);

        var items =[];

        $('.nav_list').slice(1, 4).find('a').each(function (i,element) {
            var $element = $(element);
            items.push({
                index: i,
                tag: $element.attr('href').substring($element.attr('href').indexOf('m/')+2,$element.attr('href').lastIndexOf('/')),
                title: $element.text(),
                href: $element.attr('href')
            })
        })

        console.log(items);

        spider.spider(items);
    });
})